﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//Author: ljj
//Date: 20190614
//Function: 追踪体攻击
//自带的forward是以Z轴为正方向,不好写,于是自己维护一个弧度制的方向
public class Missile : MonoBehaviour
{
    public float maxAngleSpeed = 360.0f;
    public float maxSpeed = 20.0f;
    public float acceleration = 10.0f;
    public float initSpeed = 10.0f;
    public GameObject target;
    public float forwardRad = 0.0f;
    public BaseAttack attack;
    public GameObject hitEffectPrefab;
    Vector3 targetWorldPos;
    float speed;

    MapManager mMapManager;

    // Start is called before the first frame update
    void Start()
    {
        speed = initSpeed;
        targetWorldPos = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (target != null)
            targetWorldPos = target.transform.position;

        float delta = Time.deltaTime;
        if (delta <= 0)
        {
            return;
        }
        float offsetRad = Function.GetRadIgnoreZ(targetWorldPos, transform.position);
        float diffRad = Function.DistanceBetweenRad(offsetRad, forwardRad);

        float needTime = Mathf.Abs(diffRad) / (maxAngleSpeed / Mathf.Rad2Deg);
        needTime = System.Math.Min(needTime, delta);
        //Debug.Log(offsetRad + " " + forwardRad + " " + diffRad + " " + needTime);
        float forwardRad1 = forwardRad + maxAngleSpeed / Mathf.Rad2Deg * needTime;
        float forwardRad2 = forwardRad - maxAngleSpeed / Mathf.Rad2Deg * needTime;
        if (Function.DistanceBetweenRad(forwardRad1, offsetRad) < Function.DistanceBetweenRad(forwardRad2, offsetRad))
            forwardRad = forwardRad1;
        else
            forwardRad = forwardRad2;

        speed = System.Math.Min(speed + acceleration * delta, maxSpeed);
        Vector3 forward = new Vector3(Mathf.Cos(forwardRad), Mathf.Sin(forwardRad), 0);
    

        Vector3 pos = transform.position + forward * delta * speed * ConstOfMap.xGap;
        pos.z = pos.y * 0.1f;
        transform.position = pos;
        float dis = Function.DistanceIgnoreZ(targetWorldPos, transform.position);
        if (dis < speed * delta * ConstOfMap.xGap)
        {
            if (attack is AoeAttack)
            {
                ((AoeAttack)attack).setTarget(targetWorldPos); 
            }
            attack.Attack();
            if (hitEffectPrefab != null)
            {
                EffectObjectManager.Instance.CreateEffectObject(hitEffectPrefab, targetWorldPos, transform.rotation);
            }
            gameObject.transform.position = new Vector3(-10, -10, 0);
            gameObject.SetActive(false);
            return;
        }
        if (!MapManager.Instance.IsRoad(Function.WorldPosToLogicPos(transform.position)))
        {
            if (attack is AoeAttack)
            {
                ((AoeAttack)attack).setTarget(targetWorldPos);
                attack.Attack();
            }
            if (hitEffectPrefab != null)
            {
                EffectObjectManager.Instance.CreateEffectObject(hitEffectPrefab, transform.position, transform.rotation);
                //GameObject.Instantiate(hitEffectPrefab, transform.position, transform.rotation, transform.parent);
            }
            gameObject.transform.position = new Vector3(-10, -10, 0);
            gameObject.SetActive(false);
            return;
        }
        UpdataForward();
    }

    public void InitForward()
    {
        Vector3 offset = transform.position;
        if (target != null)
            offset = (target.transform.position - transform.position).normalized;
        forwardRad = Mathf.Atan2(offset.y, offset.x);
        UpdataForward();
    }

    public void UpdataForward()
    {
        float angle = forwardRad * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0, 0, angle);
    }
}
